/*********************************************************************/
char    *name="smclock";
char    *usage="233.17.186.20 [-p 10002] [-ttl 10] [-sec 10] [-u]";

/*
 * Copyright (C) 2008 CERNET Network Center 
 * smclock/smjoin/smclock6/smjoin6/smsend6/smrec6 
 *
 * Design and coding: 
 * Xing Li <xing@cernet.edu.cn> 
 * Congxiao Bao <congxiao@cernet.edu.cn>
 *
 * Some portions of the code are copied and modified from 
 * mcastClient.c fpont 12/99 pont.net 
 * ssmping package * Copyright (C) 2005  Stig Venaas 
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */
/***************************************/
/* modified from */
/* fpont 12/99   */
/* pont.net      */
/* mcastClient.c */
/***************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h> 
#include <sys/time.h> 


#define MAXLINE 100
#define MAX_MSG 100

int timeday2();

/*********************************************************************/
int     main (argc,argv)
int     argc;
char    **argv;
{
        int     ki=0, kfi=0, kfin[10];
        char    group[MAXLINE], port[MAXLINE];
        int     sport, raw=0, sec=10, uu=0;
	unsigned long	count = 0;

/*********************************************************************/
  time_t t;
  struct tm *tm;
  char    str[100];

  int sd, rc, i;
  unsigned char ttl = 15;
  struct sockaddr_in cliAddr, servAddr;
  struct hostent *h;

  char	msg[MAX_MSG];
  int mflag = 1;

  strcpy(port, "1500");
  sport = 1501;


for (ki = 1; ki < argc; ki++) {

        if ('-' != argv[ki][0])         kfin[++kfi] = ki;

        else if (strcmp("-p", argv[ki]) == 0)   strcpy(port, argv[++ki]);
        else if (strcmp("-ttl", argv[ki]) == 0) ttl = atoi(argv[++ki]);
        else if (strcmp("-sec", argv[ki]) == 0) sec = atoi(argv[++ki]);
        else if (strcmp("-raw", argv[ki]) == 0) raw = 1;
        else if (strcmp("-u", argv[ki]) == 0) uu = 1;

        else {
                fprintf(stderr, "%s: illegal option %s \n", name, argv[ki]);
                fprintf(stderr, "Usage: %s %s \n", name, usage);
                exit(1);
        }
}

        if (argc == 1)  {
                fprintf(stderr, "Usage: %s %s \n", name, usage);
                exit (1);
        }
	 strcpy(group, argv[ kfin[1] ]);


  sport = atoi(port);

  h = gethostbyname(group);
  if(h==NULL) {
    printf("%s : unknown host '%s'\n",argv[0],group);
    exit(1);
  }

  servAddr.sin_family = h->h_addrtype;
  memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0],h->h_length);
  servAddr.sin_port = htons(sport);
  
  /* check if dest address is multicast */
  if(!IN_MULTICAST(ntohl(servAddr.sin_addr.s_addr))) {
    	mflag = 0;
  }

  /* create socket */
  sd = socket(AF_INET,SOCK_DGRAM,0);
  if (sd<0) {
    printf("%s : cannot open socket\n",argv[0]);
    exit(1);
  }
  
  /* bind any port number */
  cliAddr.sin_family = AF_INET;
  cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  cliAddr.sin_port = htons(0);
  if(bind(sd,(struct sockaddr *) &cliAddr,sizeof(cliAddr))<0) {
    perror("bind");
    exit(1);
  }

	if (mflag == 1) {
  		if(setsockopt(sd,IPPROTO_IP,IP_MULTICAST_TTL, &ttl,sizeof(ttl))<0) {
    		printf("%s : cannot set ttl = %d \n",argv[0],ttl);
    		exit(1);
  		}
	}
	else {
	}
  

  /* send data */
	while (1) {
		if (uu == 0) {
	        	time(&t);
        		tm = localtime (&t);
        		strftime (str, sizeof (str), "%Y.%m.%d %H:%M:%S %Z", tm);
		}
		else {
			timeday2(str); 
		}

		sprintf(msg, "%s  %ld", str, count++);
		if (mflag == 1)	printf("sending to group %s#%d [%s]\n", group, sport, msg); 
		else		printf("sending to host  %s#%d [%s]\n", group, sport, msg); 

    		rc = sendto(sd,
		msg,
		strlen(msg)+1,
		0,
		(struct sockaddr *) &servAddr, sizeof(servAddr));
    
    if (rc<0) {
      printf("%s : cannot send data %d\n",argv[0],i-1);
      close(sd);
      exit(1);
    }
	sleep(1);
	if (count == (long int)sec)	break;
  }/* end for */

  /* close socket and exit */
  close(sd);
  exit(0);
}


int	timeday2(daytime)
char    daytime[];
{
        struct  timeval tp;
        float sec22, sec, usec;
        time_t t;
        struct tm *tm;
        char str[100], date2[100], sec2[10], zone2[10];

        time(&t);
        tm = localtime (&t);
        strftime (str, sizeof (str), "%Y.%m.%d %H:%M:%S %Z", tm);
        strftime (date2, sizeof (date2), "%Y.%m.%d %H:%M", tm);
        strftime (sec2, sizeof (sec2), "%S", tm);
        strftime (zone2, sizeof (zone2), "%Z", tm);
        sec22 = atof(sec2);

        gettimeofday(&tp, NULL);
        sec = tp.tv_sec;
        usec = tp.tv_usec/1.0e6;

        sec22 = sec22 + usec;
        if (sec22 < 10.0)       sprintf(sec2, "0%f", sec22);
        else                    sprintf(sec2, "%f", sec22);
        sprintf(daytime, "%s:%s %s", date2, sec2, zone2);
	return (0);
}


